#include "XmlReader.h"

#include <iostream>

using namespace std;

XmlReader::XmlReader()
{
	initialize();
}

XmlReader::~XmlReader()
{
	cleanup();
}

void XmlReader::initialize()
{

}

void XmlReader::cleanup()
{

}

void XmlReader::parseConfigFile(string file, SessionData & data)
{
	QFile xmlFile(file.c_str());

	QDomDocument domDocument("document");
	domDocument.setContent(&xmlFile);

	// Parse tree

	QDomElement element = domDocument.documentElement();
	QDomNodeList domList = domDocument.childNodes();

	for( int i=0 ; i < domList.count() ; ++i )
	{
		QDomNode node = domList.item(i);
		QString name = node.nodeName();

		if( name == "session")
			parseSession(node.childNodes(), data);
	}
}

void XmlReader::parseCodecFile(string file, SdpData & data)
{
	QFile xmlFile(file.c_str());

	QDomDocument domDocument("document");
	domDocument.setContent(&xmlFile);

	// Parse tree

	QDomElement element = domDocument.documentElement();
	QDomNodeList domList = domDocument.childNodes();

	for( int i=0 ; i < domList.count() ; ++i )
	{
		QDomNode node = domList.item(i);
		QString name = node.nodeName();

		if( name == "codecs")
			parseCodecs(node.childNodes(), data);
	}
}

void XmlReader::parseSession(QDomNodeList list, SessionData & data)
{
	for( int i=0 ; i < list.count() ; ++i )
	{
		QDomNode node = list.item(i);
		QString name = node.nodeName();

		if( name == "settings" )
			parseSettings(node.childNodes(), data);

		else if( name == "contacts" )
			parseContacts(node.childNodes(), data);
	}
}

void XmlReader::parseSettings(QDomNodeList list, SessionData &data)
{
	for( int i=0 ; i < list.count() ; ++i )
	{
		QDomNode node = list.item(i);
		QString name = node.nodeName();

		if( name == "user" )
			data.user = node.childNodes().item(0).nodeValue().toStdString();

		else if( name == "password" )
			data.password = node.childNodes().item(0).nodeValue().toStdString();

		else if( name == "server" )
			data.server = node.childNodes().item(0).nodeValue().toStdString();;
	}
}

void XmlReader::parseContacts(QDomNodeList list, SessionData &data)
{
	for( int i=0 ; i < list.count() ; ++i )
	{
		QDomNode node = list.item(i);
		QString name = node.nodeName();

		if( name == "contact" )
			parseContact(node.childNodes(), data);
	}
}

void XmlReader::parseContact(QDomNodeList list, SessionData &data)
{
	ContactData* contactData = new ContactData();
	contactData->online = false;

	for( int i=0 ; i < list.count() ; ++i )
	{
		QDomNode node = list.item(i);
		QString name = node.nodeName();

		if( name == "user" )
			contactData->user = node.childNodes().item(0).nodeValue().toStdString();

		else if( name == "nick" )
			contactData->nick = node.childNodes().item(0).nodeValue().toStdString();
	}

	data.contacts.push_back(contactData);
}

void XmlReader::parseCodecs(QDomNodeList list, SdpData & data)
{
	for( int i=0 ; i < list.count() ; ++i )
	{
		QDomNode node = list.item(i);
		QString name = node.nodeName();

		if( name == "codec" )
			parseCodec(node, data);
	}
}

void XmlReader::parseCodec(QDomNode node, SdpData & data)
{
	QDomNamedNodeMap map = node.attributes();
	CodecData* codecData = new CodecData();

	for( int i=0 ; i < map.size() ; ++i )
	{
		QDomNode node = map.item(i);
		QString name = node.nodeName();
		QString value = node.nodeValue();

		if( name == "type" )
		{
			if( value == "audio" )
				codecData->type = AUDIO;
			else if( value == "video" )
				codecData->type = VIDEO;
		}

		if( name == "payoff" )
			codecData->payload = value.toUShort();

		if( name == "name" )
			codecData->name = value.toStdString();
	}

	data.m_codecs.push_back(codecData);
}